'''
坐标变换
计算病灶坐标基于肺部中心的相对位置
'''
import csv
import pandas
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler, MaxAbsScaler


# 读取数据
def read_data(data_path):
    data = []
    f = csv.reader(open(data_path, 'r'))
    for i in f:
        data.append(i)
    return data


# 生成all_data6.csv
def get_center_coord():
    data_path = 'D:/lung_cancer/data/all_data5.csv'
    data = read_data(data_path)

    new_data = []
    data[0].append('newX')
    data[0].append('newY')
    for line in data[1:]:
        patient = line[1]
        x = int(line[3])
        y = int(line[4])
        center_x = float(line[42])
        center_y = float(line[43])

        newX = x/512.0-center_x
        newY = y/512.0-center_y

        line.append(newX)
        line.append(newY)
        new_data.append(line)

        print('%s is completed'%(patient))

    df = pandas.DataFrame(new_data, columns=data[0])
    df.to_csv('D:/lung_cancer/data/all_data6.csv', index=False)


# 取上限值归一化所有特征，生成all_data7.csv
# 废弃不用
def normalize_feature():
    data_path = 'D:/lung_cancer/data/all_data6.csv'
    data = read_data(data_path)

    new_data = []

    for line in data[1:]:
        patient = line[1]
        line[2] = int(line[2])/200.0
        line[3] = int(line[3])/512.0
        line[4] = int(line[4])/512.0
        line[5] = int(line[5])/100.0
        line[19] = int(line[19])/200.0
        line[21] = int(line[21])/100.0
        line[22] = int(line[22])/200.0
        line[37] = float(line[37])/50.0
        line[38] = float(line[38])/10.0
        line[39] = float(line[39])/20.0
        line[40] = float(line[40])/20.0
        line[41] = float(line[41])/200.0
        new_data.append(line)

        print('%s is completed' % (patient))

    df = pandas.DataFrame(new_data, columns=data[0])
    df.to_csv('D:/lung_cancer/data/all_data7.csv', index=False)



if __name__ == '__main__':
    print('hello')
    get_center_coord()
    normalize_feature()

